var express=require('express');
var router=express.Router();

const http = require('http');
const url = require('url');
const sql = require('mysql');

var TEST_DATABASE = 'bigevent';  
var TEST_TABLE = 'bigevent'; 

var con = sql.createConnection({
    host: 'localhost',
    user:'root',
    password:'694310908',
    database:TEST_DATABASE
});

con.connect();

function onRequest(req,res){
    console.log("new Request: getSummary");
    var arg1 = url.parse(req.url,true).query;
    // console.log(arg1.year);

    con.query('select id,year(date) as year,month(date) as month,day(date) as day,name,type,summary,postimg from bigevent where year(date) = ?',[arg1.year],function(e,sqlres,f){
        if(e){
            throw e;
        }
        var summarylist =[];
        if(sqlres){
            for(var i=0;i<sqlres.length;i++){
                summarylist.push({'id':sqlres[i].id,'year':sqlres[i].year,'month':sqlres[i].month,'day':sqlres[i].day,'name':sqlres[i].name,'type':sqlres[i].type,'summary':sqlres[i].summary,'postimg':sqlres[i].postimg});
                // console.log(sqlres[i].name);
            }
            res.writeHead(200,{'Access-Control-Allow-Origin':'*','Content-Language':'en,zh','Content-Type': 'application/json; charset=utf-8'});

            res.end(JSON.stringify({'list':summarylist}));
        }

    });

    
}

router.get('/',onRequest );

module.exports=router;